var version = "0.0.1";
var xhrXML = Titanium.Network.createHTTPClient();

function js_yyyy_mm_dd() {
  now = new Date();
  year = "" + now.getFullYear();
  month = "" + (now.getMonth() + 1); if (month.length == 1) { month = "0" + month; }
  day = "" + now.getDate(); if (day.length == 1) { day = "0" + day; }
  hour = "" + now.getHours(); if (hour.length == 1) { hour = "0" + hour; }
  minute = "" + now.getMinutes(); if (minute.length == 1) { minute = "0" + minute; }
  second = "" + now.getSeconds(); if (second.length == 1) { second = "0" + second; }
  return year + "-" + month + "-" + day;
}

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*)(?::([^:@]*))?)?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};

function getVersion(server) {
	//Ti.API.info("The Server is: " + server);
	var versionUrl = "http://"+server+"/api.php?command=version";
	xhrXML.open("GET",versionUrl,false);
	xhrXML.send();	
}


xhrXML.onload = function() {
	if (this.status == 200) {
		var version = -1;
		var status = -1;
		//Ti.API.info(this.responseText);
		var xml = Ti.XML.parseString(this.responseText);
		var xml_doc = xml.documentElement.getElementsByTagName("result");
		for (var i=0;i<xml_doc.length;i++) {
			version = xml_doc.item(i).getElementsByTagName("output").item(0).text;
			status =  xml_doc.item(i).getElementsByTagName("status").item(0).text;
		}
		if (status == "Success") {
			var uri = parseUri(this.location);
			var host = uri['host'];
			Ti.API.info("Version: " + version + " Host: " + host);
		}
	}
};

var view_width = Ti.Platform.displayCaps.platformWidth - 20;
var WindowHeight = Ti.Platform.displayCaps.platformHeight;
var WindowWidth = Ti.Platform.displayCaps.platformWidth;
